package com.deng.cloud.controller;


import com.deng.cloud.service.DeptService;
import com.deng.cloud.pojo.Dept;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;


    @PostMapping("/dept/add")
    public boolean addDept(@RequestBody Dept dept){
        return deptService.addDept(dept);
    }


    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "hystrixGet")
        public Dept get(@PathVariable("id") Long id){
            Dept dept = deptService.queryById(id);
            if (dept==null){
                throw new RuntimeException("Fail");
            }
            return dept;
        }

        public Dept hystrixGet(@PathVariable("id") Long id){
            return new Dept().setDeptno(id).setDname("此id"+id+":不存在或者无法查询").setDb_source("this is no DataSource in theMySQL");
    }


    @GetMapping("/dept/list")
    public List<Dept> queryAll(){
        return deptService.queryAll();
    }


}
